//
//  SportDetailReportModel.m
//  CoolTennisBall
//
//  Created by 谢伟康 on 16/1/7.
//  Copyright © 2016年 CoolLang. All rights reserved.
//

#import "SportDetailReportModel.h"
#import "SportDetailItemModel.h"

@interface SportDetailReportModel ()
@property (nonatomic, strong, readwrite) NSMutableArray *avgTypeSpeeds;
@property (nonatomic, strong, readwrite) NSMutableArray *maxTypeSpeeds;

@end

@implementation SportDetailReportModel

- (void)configureModelWithDic:(NSDictionary *)dic
{
    self.pingJiCount = [dic[@"TypeCount"][@"PingJi"] integerValue];
    self.xuanQiuCount = [dic[@"TypeCount"][@"XuanQiu"] integerValue];
    self.xueQiuCount = [dic[@"TypeCount"][@"XueQiu"] integerValue];
    self.jieJiCount = [dic[@"TypeCount"][@"JieJi"] integerValue];
    self.kouShaCount = [dic[@"TypeCount"][@"GaoYa"] integerValue];
    self.faQiuCount = [dic[@"TypeCount"][@"FaQiu"] integerValue];
    self.calories = [dic[@"Caroline"] integerValue];
    self.maxSpeed = [dic[@"MaxSpeed"] integerValue];
    self.userId = dic[@"UserID"];
    self.dateString = dic[@"Date"];
    self.totalScore = [dic[@"TotalScore"] integerValue];
    self.speedPercent = [dic[@"SpeedPercent"] floatValue];
    self.durationPercent = [dic[@"DurationPercent"] floatValue];
    self.hitPercent = [dic[@"HitPercent"] floatValue];
    self.effectiveSportTime = [dic[@"Duration"] integerValue];
    self.forehandPercent = [dic[@"ForeHandPercent"] integerValue];
    self.backhandPercent = [dic[@"BackHandPercent"] integerValue];
    
    self.detailDesArray = [@[] mutableCopy];
    self.avgTypeSpeeds = [@[] mutableCopy];
    self.maxTypeSpeeds = [@[] mutableCopy];
    
    for (NSDictionary *detailDic in dic[@"ReportInfo"]) {
        SportDetailItemModel *model = [[SportDetailItemModel alloc] init];
        [model configureModelWithDic:detailDic];
        [self.detailDesArray addObject:model];
    }
    
    if (dic[@"AvgTypeSpeed"] != nil && ![dic[@"AvgTypeSpeed"] isKindOfClass:[NSNull class]]) {
        
        if ([dic[@"AvgTypeSpeed"] isKindOfClass:[NSArray class]]) {
            for (id avgTypeSpeed in dic[@"AvgTypeSpeed"]) {
                if ([avgTypeSpeed isKindOfClass:[NSString class]]) {
                    SportDetailSpeedItem *speedItem = [[SportDetailSpeedItem alloc] initWithForehandSpeed:avgTypeSpeed backhandSpeed:avgTypeSpeed];
                    [self.avgTypeSpeeds addObject:speedItem];
                    
                } else if ([avgTypeSpeed isKindOfClass:[NSArray class]]) {
                    SportDetailSpeedItem *speedItem = [[SportDetailSpeedItem alloc] initWithForehandSpeed:avgTypeSpeed[0] backhandSpeed:avgTypeSpeed[1]];
                    [self.avgTypeSpeeds addObject:speedItem];
                }
            }
        }else {
            if ([dic[@"AvgTypeSpeed"] isKindOfClass:[NSString class]]) {
                 SportDetailSpeedItem *speedItem = [[SportDetailSpeedItem alloc] initWithForehandSpeed:dic[@"AvgTypeSpeed"] backhandSpeed:dic[@"AvgTypeSpeed"]];
                 [self.avgTypeSpeeds addObject:speedItem];
            }
        }
        
    }
    
    if (dic[@"MaxTypeSpeed"] != nil && ![dic[@"AvgTypeSpeed"] isKindOfClass:[NSNull class]]) {
        
        for (id maxTypeSpeed in dic[@"MaxTypeSpeed"]) {
            if ([maxTypeSpeed isKindOfClass:[NSString class]]) {
                
                SportDetailSpeedItem *speedItem = [[SportDetailSpeedItem alloc] initWithForehandSpeed:maxTypeSpeed backhandSpeed:maxTypeSpeed];
                [self.maxTypeSpeeds addObject:speedItem];
                
            } else if ([maxTypeSpeed isKindOfClass:[NSArray class]]) {
                SportDetailSpeedItem *speedItem = [[SportDetailSpeedItem alloc] initWithForehandSpeed:maxTypeSpeed[0] backhandSpeed:maxTypeSpeed[1]];
                [self.maxTypeSpeeds addObject:speedItem];
            }
        }
    }
}

- (NSInteger)amountOfCount
{
    NSInteger amount = self.pingJiCount + self.xuanQiuCount + self.xueQiuCount + self.jieJiCount + self.kouShaCount + self.faQiuCount;
    return amount;
}

- (NSString *)configureSportStyle
{
    NSString * sportStyle = nil;
    CGFloat ratio = 0.0f;
    ratio = (CGFloat)(self.jieJiCount + self.kouShaCount)/(self.kouShaCount + self.jieJiCount + self.xueQiuCount + self.xuanQiuCount + self.pingJiCount);
    if (ratio < 0.2) {
        sportStyle = NSLocalizedString(@"baseline type", nil);
    }else if (ratio > 0.4) {
        sportStyle = NSLocalizedString(@"serve & volley type", nil);
    }else{
        sportStyle = NSLocalizedString(@"comprehensive type", nil);
    }

    return sportStyle;
}

/**
 *  爆发力描述
 *
 *  @return 爆发力描述
 */
- (NSString *)powerScoreDescription
{
    NSInteger percent = roundf(self.speedPercent * 100);
    
    if (percent >= 90) {
        return NSLocalizedString(@"Power.Perfect", nil);
    } else if (percent >= 80) {
        return NSLocalizedString(@"Power.Nice", nil);
    } else if (percent >= 60) {
        return NSLocalizedString(@"Power.Medium", nil);
    } else {
        return NSLocalizedString(@"Power.Weak", nil);
    }
}

/**
 *  耐久力描述
 *
 *  @return 耐久力描述
 */
- (NSString *)enduranceScoreDescription
{
    NSInteger percent = roundf(self.durationPercent * 100);
    
    if (percent >= 90) {
        return NSLocalizedString(@"Endurance.Perfect", nil);
    } else if (percent >= 80) {
        return NSLocalizedString(@"Endurance.Good", nil);
    } else if (percent >= 60) {
        return NSLocalizedString(@"Endurance.Medium", nil);
    } else {
        return NSLocalizedString(@"Endurance.Weak", nil);
    }
}

/**
 *  运动强度描述
 *
 *  @return 运动强度描述
 */
- (NSString *)intensityScoreDescription
{
    NSInteger percent = roundf(self.hitPercent * 100);
    
    if (percent >= 80) {
        return NSLocalizedString(@"Intensity.High", nil);
    } else if (percent >= 60) {
        return NSLocalizedString(@"Intensity.Medium", nil);
    } else {
        return NSLocalizedString(@"Intensity.Low", nil);
    }
}

@end

@implementation SportDetailSpeedItem

- (instancetype)initWithForehandSpeed:(NSString *)forehandSpeed
                        backhandSpeed:(NSString *)backhandSpeed
{
    self = [super init];
    if (self) {
        _forehandSpeed = forehandSpeed;
        _backhandSpeed = backhandSpeed;
    }
    return self;
}

@end
